package com.stxx.assistant.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.stxx.assistant.domain.SysConfig;
import com.stxx.assistant.framwork.constants.LogicType;
import com.stxx.assistant.framwork.domain.model.PageQuery;
import com.stxx.assistant.framwork.domain.model.PageResult;
import com.stxx.assistant.framwork.utils.DbMetaUtils;
import com.stxx.assistant.framwork.utils.PermissionUtils;
import com.stxx.assistant.framwork.utils.StoreUtils;
import com.stxx.assistant.service.SysConfigService;
import lombok.Cleanup;
import org.noear.snack.ONode;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Init;

import java.sql.*;
import java.util.Date;
import java.util.List;

/**
 * @author Fuzhenlei
 * @date 2025/01/15 15:06
 */
@Component
public class SysConfigServiceImpl implements SysConfigService {


    @Override
    public int insert(SysConfig bean) throws SQLException {
        return StoreUtils.insert(bean);
    }

    @Override
    public int update(SysConfig bean) throws SQLException {
        return StoreUtils.update(bean,SysConfig::getId);
    }


    @Override
    public int delete(String name) throws SQLException {
        return StoreUtils.delete(SysConfig.class,SysConfig::getId,name);
    }

    @Override
    public int deleteByIds(String[] ids) throws SQLException {
        return StoreUtils.deleteByIds(SysConfig.class,SysConfig::getId,ids);
    }

    @Override
    public List<SysConfig> list(SysConfig obj) throws SQLException {
        return StoreUtils.selectList(SysConfig.class,obj);
    }

    @Override
    public PageResult<SysConfig> pageList(SysConfig obj, PageQuery pageQuery) throws SQLException {
        return StoreUtils.selectPageList(SysConfig.class,obj,pageQuery);
    }

    @Override
    public int deleteBefore(Date endDate) throws SQLException {
        return 0;
    }

    @Override
    public <T> T config(String id, Class<T> clazz) {
        if(!configMaps.containsKey(id)){
            try {
                SysConfig sysConfig = StoreUtils.selectOne(SysConfig.class,SysConfig::getId,id);
                if(sysConfig != null){
                    configMaps.put(id, Convert.convert(clazz ,sysConfig.getValue()));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        Object r = configMaps.get(id);
        return r==null?null:(T)r;
    }
}
